package com.yft.util;


import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.TextLayout;
import java.awt.geom.AffineTransform;
import java.awt.geom.Line2D;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * ��֤�빤����
 * @author SalayXu
 *
 */
public class ValidateCodeUtil {

	private static char[] codeset;

	public char[] getCodeset() {
		return codeset;
	}

	public static void setCodeset() {
		codeset = new char[62];
		for (int i = 0, k = 48; i < 10; i++) {
			codeset[i] = (char) k;
			k++;
		}
		for (int i = 10, k = 65; i < 36; i++, k++) {
			codeset[i] = (char) k;
		}
		for (int i = 36, k = 97; i < 62; i++, k++) {
			codeset[i] = (char) k;
		}
	}

	public static String getValidateCode() {
		setCodeset();
		Random random = new Random();
		String code = "";
		for (int i = 0; i < 4; i++) {
			char signcode = codeset[random.nextInt(62)];
			String rand = String.valueOf(signcode);
			code += rand;
		}
		// DesEncrypt des = new DesEncrypt(Constant.EncryptKey_fileurl);
		// code=des.getEncString(code);
		return code;
	}
	
	public static BufferedImage getValidateImg(String valueStr,int width,int height) throws Exception {			
		
		
		// ���ڴ��д���ͼ��
		if(width<=0 || width>500) width = 100;
		if(height<=0 || height>200) height = 40;
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g2 = image.createGraphics();
		
		// �趨����ɫ
		g2.setColor(Color.white);
		g2.fillRect(0, 0, width, height);
		
		// ������5�������ߣ�ʹͼ���е���֤�벻�ױ��������̽�⵽
		Random random = new Random();
		for (int i=0;i<5;i++)
		{
			int x = random.nextInt(width);
			int y = random.nextInt(5);
			int xl = random.nextInt(80);
			int yl = random.nextInt(80);
			g2.setColor(Color.black);
			g2.drawLine(x,y,x+xl,y+yl);
		}
		
		//���߿�
		g2.setColor(new Color(0,0,0));
		g2.drawRect(0,0,width-1,height-1);
		
		//�趨����
		AffineTransform at = new AffineTransform();
		AffineTransform fontAT = new AffineTransform();
		FontRenderContext frc = g2.getFontRenderContext();
	    Font theFont = new Font("Arial", Font.BOLD, 28);	    
		
	    //�趨��ʼλ��
		at.setToTranslation(10,height/1.3);
		
		if(valueStr==null || valueStr.length()<=0)
			return null;				
			
		Font theDerivedFont = theFont.deriveFont(fontAT);
		TextLayout tstring2 = new TextLayout(valueStr, theDerivedFont, frc);			
        Shape shape = tstring2.getOutline(at);
        g2.setClip(shape);
        g2.setColor(Color.blue);
        g2.fill(shape.getBounds());	        
		
        g2.setColor(Color.orange);
        for (int j = shape.getBounds().y; j < shape.getBounds().y + shape.getBounds().height; j=j+3) {
            Line2D line = new Line2D.Float( 0.0f, (float) j, (float) width, (float) j);
            g2.draw(line);
        }		
		g2.dispose();
		// ���ͼ��ҳ��,ҳ�淵��img src 
		//ImageIO.write(image, "JPEG", response.getOutputStream());	
		return image;		
	}
}
